package com.cn.mobileproject.db.dao.impl;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.cn.mobileproject.bean.Dictionary;
import com.cn.mobileproject.db.Query;
import com.cn.mobileproject.db.RowMapper;
import com.cn.mobileproject.db.SqliteTemplate;
import com.cn.mobileproject.db.UDatabase;
import com.cn.mobileproject.db.dao.DictionaryDao;
import com.cn.mobileproject.db.table.DictionaryTable;

public class DictionaryDaoImpl implements DictionaryDao {
	
	private SqliteTemplate sqliteTemplate;

	public DictionaryDaoImpl() {
		sqliteTemplate = new SqliteTemplate();
	}

	@Override
	public void insert(Dictionary mDictionary) {

	}

	@Override
	public void insertDictionarys(ArrayList<Dictionary> list) {
		if(list==null||list.size()==0) return ;
		try{
			SQLiteDatabase db = UDatabase.getDb(true);
			db.beginTransaction();
			for(Dictionary ou: list){
				db.replace(DictionaryTable.TABLE_NAME, null, dictionaryToValues(ou));
			}
			db.setTransactionSuccessful();
			db.endTransaction();
		}catch(Exception e){
		}

	}

	@Override
	public void delete() {
		Query query = new Query();
		query.from(DictionaryTable.TABLE_NAME);
		sqliteTemplate.delete(query);
	}

	@Override
	public List<Dictionary> search(String type) {
		Query query = new Query();
		query.from(DictionaryTable.TABLE_NAME).where(DictionaryTable.FIELD_TYPE+ "=?", type);
		return sqliteTemplate.queryForList(query,new DictionaryMapper());
	}
	
	private ContentValues dictionaryToValues(Dictionary dictionary) {
		ContentValues v = new ContentValues();
		v.put(DictionaryTable.FIELD_INNERTYPE, dictionary.innerType);
		v.put(DictionaryTable.FIELD_NAEM, dictionary.name);
		v.put(DictionaryTable.FIELD_NOTE, dictionary.note);
		v.put(DictionaryTable.FIELD_ORDER, dictionary.innerOrder);
		v.put(DictionaryTable.FIELD_TYPE, dictionary.type);
		v.put(DictionaryTable.FIELD_VALUE, dictionary.value);
		return v;
	}

	private static final class DictionaryMapper implements
			RowMapper<Dictionary> {
		public Dictionary mapRow(Cursor cursor, int rowNum) {
			Dictionary dictionary = new Dictionary();
			if (cursor != null && cursor.getCount() > 0) {
				dictionary.innerOrder=cursor.getInt(cursor.getColumnIndex(DictionaryTable.FIELD_ORDER));
				dictionary.innerType=cursor.getString(cursor.getColumnIndex(DictionaryTable.FIELD_INNERTYPE));
				dictionary.name=cursor.getString(cursor.getColumnIndex(DictionaryTable.FIELD_NAEM));
				dictionary.note=cursor.getString(cursor.getColumnIndex(DictionaryTable.FIELD_NOTE));
				dictionary.type=cursor.getString(cursor.getColumnIndex(DictionaryTable.FIELD_TYPE));
				dictionary.value=cursor.getString(cursor.getColumnIndex(DictionaryTable.FIELD_VALUE));
			}
			return dictionary;
		}
	}

	@Override
	public List<Dictionary> search() {
		Query query = new Query();
		query.from(DictionaryTable.TABLE_NAME);
		return sqliteTemplate.queryForList(query,new DictionaryMapper());
	}

	@Override
	public Dictionary searchFirstType(String type) {
		Query query = new Query();
		query.from(DictionaryTable.TABLE_NAME).where(DictionaryTable.FIELD_TYPE+ "=?", type).limit("0,1");
		List<Dictionary> list =  sqliteTemplate.queryForList(query,new DictionaryMapper());
		if(list!=null&&list.size()>0){
			return list.get(0);
		}
		return null;
	}

}
